package fitnesshub.controller;

import fitnesshub.pojo.dto.AddCoachDTO;
import fitnesshub.pojo.dto.CoachPageQueryDTO;
import fitnesshub.pojo.entity.Coach;
import fitnesshub.result.PageResult;
import fitnesshub.result.Result;
import fitnesshub.service.CoachService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 教练管理
 */
@RestController
@RequestMapping("/admin/coach")
@Slf4j
public class CoachController {
    @Autowired
    private CoachService coachService;

    /**
     * 教练分页
     * @param coachPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CoachPageQueryDTO coachPageQueryDTO) {
        log.info("教练分页查询，参数：{}", coachPageQueryDTO);
        PageResult pageResult = coachService.page(coachPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询教练
     * @param coachId
     * @return
     */
    @GetMapping("/{coachId}")
    public Result<Coach> getById(@PathVariable Integer coachId) {
        log.info("根据id:{},查询教练", coachId);
        Coach coach = coachService.getById(coachId);
        return Result.success(coach);
    }

    /**
     * 新增教练
     * @param addCoachDTO
     * @return
     */
    @PostMapping
    public Result addACoach(@RequestBody AddCoachDTO addCoachDTO) {
        log.info("新增教练:{}", addCoachDTO);
        coachService.addACoach(addCoachDTO);
        return Result.success();
    }

    /**
     * 编辑教练
     * @param coach
     * @return
     */
    @PutMapping
    public Result editCoach(@RequestBody Coach coach) {
        log.info("编辑教练:{}", coach);
        coachService.editCoach(coach);
        return Result.success();
    }

    /**
     * 根据id删除教练
     * @param coachId
     * @return
     */
    @DeleteMapping("{coachId}")
    public Result deleteById(@PathVariable Integer coachId) {
        log.info("删除教练,id:{}", coachId);
        coachService.deleteById(coachId);
        return Result.success();
    }

    /**
     * 检查教练是否存在
     * @param coachId
     * @return
     */
    @GetMapping("/exists/{coachId}")
    public Result<Boolean> checkCoachExists(@PathVariable Integer coachId) {
        log.info("检查教练是否存在, id:{}", coachId);
        boolean exists = coachService.checkCoachExists(coachId);
        return Result.success(exists);
    }
}
